var sessionid = $.cookie("wsessionId");
$(function(){
	$.getScript("/AllJs/formhandle.js",function(){
		var systemname = "system";//系统类型可以写死
		var divs = $("#divs");
		
		//window1(parentId,poppeId,titlename,poppetitleid,poppecontentid,zindex,isclose)
		window1("divs","showcontent","流程设置","showcontentt","showcontentc",20,60,800,430,600,false);
		window1("divs","flowdatas","工作流数据表","flowdatast","flowdatasc",30,80,1200,400,600,false);
		window1("divs","flowall","工作流总设置","flowallt","flowallc",50,500,500,700,120,false);
		var showcontentc = $("#showcontentc");
		var form = $("<form id='flowsetform'></form>");
		var nameP = $("<div style='width:100%;height:42px;padding:10px 0 0 20%;'><span>节点名字:</span>" +
				"<input type='text' name='name' style='margin-left:20px;' /></div>");
		var statusP = $("<div style='width:100%;height:42px;padding:10px 0 0 20%;'><span>状态:</span>" +
				"<input type='text' name='status' style='margin-left:20px;' readonly /></div>");
		var userP = $("<div style='width:100%;height:42px;padding:10px 0 0 20%;'><span>人员:</span>" +
				"<select name='user'></select></div>");
		var deptP = $("<div style='width:100%;height:42px;padding:10px 0 0 20%;'><span>部门:</span>" +
				"<select name='dept'></select></div>");
		var roleP = $("<div style='width:100%;height:42px;padding:10px 0 0 20%;'><span>角色:</span>" +
				"<select name='role'></select></div>");
		form.append(nameP);
		form.append(statusP);
		form.append(userP);
		form.append(deptP);
		form.append(roleP);
		showcontentc.append(form);
		
		var flownameP = $("<div style='width:100%;height:42px;padding:10px 0 0 20%;'><span>流程名称:</span>" +
			"<input type='text' name='flowname' style='margin-left:20px;' /></div>");
		var flowidP = $("<input type='hidden' name='flowid'>");
		var saveBt = $("<div style='margin-left:40%;'><button>提交</button></div>");
		$("#flowallc").append(flownameP);
		$("#flowallc").append(flowidP);
		$("#flowallc").append(saveBt);
		flownameP.find("input").blur(function(){
			wioflow.setTitle(flownameP.find("[name = 'flowname']").val());
		});
		saveBt.click(function(){
			var flowid = flowidP.val();
			var path="";
			var senddata;
			var flowjson = wioflow.exportData();
			if(flowid == undefined || flowid == ''){
				path = "saveFlow";
				var senddata = {'jsonData':JSON.stringify(flowjson)};
			}else{
				path = "updateFlow";
				senddata = {'jsonData':JSON.stringify(flowjson),'flowid':flowid};
			}
			$.ajax({
				url:'/'+path+'?sessionId='+sessionid,
				data:senddata,
				type:'post',
				dataType:'json',
				success:function(data){
					if(data['type'] == 'success'){
						alert("操作成功");
						$("#flowallc").find("[name='flowid']").val(data['flowid']);
					}else{
						alert(data['data']);
					}
				}
			});
		})
		
		$.ajax({
			url:'/user_getAllUser?sessionId='+sessionid,
			type:'get',
			async:false,
			dataType:'json',
			success:function(data){
				var userDatas = data['userDatas'];
				for(var i=0;i<userDatas.length;i++){
					if(userDatas[i]['isend'] == "0"){
						continue;
					}
					userP.find("select").append("<option value='"+userDatas[i]['id']+"'>"+userDatas[i]['name']+"</option>");
				}
			}
		});
		
		$.ajax({
			url:'/dept_getAllDept?sessionId='+sessionid,
			type:'get',
			async:false,
			dataType:'json',
			success:function(data){
				var deptDatas = data['deptDatas'];
				for(var i=0;i<deptDatas.length;i++){
					if(deptDatas[i]['isend'] == "0"){
						continue;
					}
					deptP.find("select").append("<option value='"+deptDatas[i]['id']+"'>"+deptDatas[i]['name']+"</option>");
				}
			}
		});
		
		$.ajax({
			url:'/role_getAllRole?sessionId='+sessionid,
			type:'get',
			async:false,
			dataType:'json',
			success:function(data){
				var roleDatas = data['roleDatas'];
				for(var i=0;i<roleDatas.length;i++){
					if(roleDatas[i]['isend'] == "0"){
						continue;
					}
					roleP.find("select").append("<option value='"+roleDatas[i]['id']+"'>"+roleDatas[i]['name']+"</option>");
				}
			}
		});
		
		wioflow.onItemFocus = function(id,type){
			if(type == 'node'){
				nameP.find("input").attr('readonly',false);
				statusP.find("input").attr('readonly',true);
				userP.find("select").attr('disabled',false);
				deptP.find("select").attr('disabled',false);
				roleP.find("select").attr('disabled',false);
				
				var cn = wioflow.$nodeData;
				clearData("flowsetform");
				var cnid = cn[id];
				fillForm_new(form,cnid);
				form.find("[name]").each(function(){
					$(this).unbind();
					var cname = $(this).attr('name');
					if(cname == 'status'){
						return true;
					}
					if($(this).is("input")){
						$(this).blur(function(){
							cnid[cname] = $(this).val();
						});
					}else if($(this).is("select")){
						$(this).change(function(){
							cnid[cname] = $(this).find("option:selected").val();
						})
					}
				});
			}else if(type == 'line'){
				nameP.find("input").attr('readonly',true);
				statusP.find("input").attr('readonly',false);
				userP.find("select").attr('disabled','disabled');
				deptP.find("select").attr('disabled','disabled');
				roleP.find("select").attr('disabled','disabled');
				clearData("flowsetform");
				
				var cn = wioflow.$lineData;
				form.find("[name='status']").unbind();
				form.find("[name='status']").blur(function(){
					cn[id]['status'] = $(this).val();
				});
				form.find("[name='status']").val(cn[id]['status']);
			}
			return true;
		}
		$("#wioflow").click(function(){
			var title = wioflow.$title;
			flownameP.find("input").val(title);
		})
	})
	$.ajax({
		url:'/getAllFlowBean?sessionId='+sessionid,
		type:'get',
		dataType:'json',
		success:function(data){
			var flowdatasc = $("#flowdatasc");
			for(var i=0;i<data.length;i++){
				var allFlowDiv = $("<div style='width:100%;height:30px;cursor:pointer;text-align:center;' flowid='"+data[i]['id']+"'>"
						+data[i]['flowname']+"</div>");
				flowdatasc.append(allFlowDiv);
				allFlowDiv.click(function(){
					var flowid = $(this).attr('flowid');
					$.ajax({
						url:'/getFlowFromId?sessionId='+sessionid,
						type:'get',
						data:{'flowid':flowid},
						dataType:'json',
						success:function(data){
							wioflow.clearData();
							wioflow.loadData(data);
							wioflow.$lineData = data.lines;
							$("#flowallc").find("[name='flowid']").val(flowid);
						}
					})
				})
			}
		}
	});
});
















